package 查找.树.二叉树.测试;

/**
 * 经典二叉树，一个根节点只有左右两个子节点
 */
public class BinaryTree {

    public Object data;

    public BinaryTree leftChild;

    public BinaryTree rightChild;

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public BinaryTree getLeftChild() {
        return leftChild;
    }

    public void setLeftChild(BinaryTree leftChild) {
        this.leftChild = leftChild;
    }

    public BinaryTree getRightChild() {
        return rightChild;
    }

    public void setRightChild(BinaryTree rightChild) {
        this.rightChild = rightChild;
    }

    public BinaryTree(Object data) {
        this.data = data;
        this.leftChild = null;
        this.rightChild = null;
    }
    public BinaryTree() {}

    /**
     *     插入左子树
     *     若当前节点非空，则在当前节点的左子树插入一个结点
     *     若插入成功，则返回新插入结点的引用
     */
    public BinaryTree insertLeft(BinaryTree currBinaryTree, Object data) {
        if (currBinaryTree == null) {
            return null;
        }
        BinaryTree leftTree = new BinaryTree(data);
        currBinaryTree.setLeftChild(leftTree);
        return currBinaryTree.getLeftChild();
    }

    /**
     *     插入右子树
     */
    public BinaryTree insertRight(BinaryTree currBinaryTree, Object data) {
        if (currBinaryTree == null) {
            return null;
        }
        BinaryTree rightTree = new BinaryTree(data);
        currBinaryTree.setRightChild(rightTree);
        return currBinaryTree.getRightChild();
    }

    /**
     * 遍历
     */
    public void iterator(BinaryTree binaryTree) {
        if (binaryTree == null) {
            return;
        }
        System.out.println("data is : " + binaryTree.getData());
        iterator(binaryTree.getLeftChild());
        iterator(binaryTree.getRightChild());
    }
}
